package com.zhihuichongcheng.application.ui.task;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.task.adapter.FilterAdapter;

import java.util.HashMap;
import java.util.List;

/**
 * task Filter Created by LK on 2017/1/6.
 */
public class TaskFilterActivity extends BaseActivity {
    public static final int REQUEST_CODE_GET_FILTER = 1001;

    public static final String EXTRA_RESULT_USER_IDS = "extra_result_user_ids";
    public static final String EXTRA_RESULT_TASK_TYPE = "extra_result_mission_type";
    public static final String EXTRA_RESULT_PRIORITY = "extra_result_priority";
    public static final String EXTRA_RESULT_FINISH_TIME = "extra_result_finish_time";
    public static final String EXTRA_LIST_TYPE = "extra_list_type";

    public static final String EXTRA_DEFAULT_TASK_TYPE = "extra_default_task_type";
    public static final String EXTRA_DEFAULT_USER_IDS = "extra_default_user_ids";
    public static final String EXTRA_DEFAULT_TASK_LV = "extra_default_task_lv";
    public static final String EXTRA_DEFAULT_FINISH_TIME = "extra_default_task_finish_time";
    protected Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            }
        }
    };
    protected HttpManager manager;
    private XRecyclerView mRecyclerView;
    private FilterAdapter mAdapter;
    private int listType;

    //默认数据
    private int defaultTaskType = -1;
    private String defaultUserIds = "";
    private int defaultTaskLv = -1;
    private String defaultFinishTime = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            if (bundle.containsKey(EXTRA_LIST_TYPE)) {
                listType = bundle.getInt(EXTRA_LIST_TYPE);
            }
            if (bundle.containsKey(EXTRA_DEFAULT_TASK_TYPE)) {
                defaultTaskType = bundle.getInt(EXTRA_DEFAULT_TASK_TYPE, -1);
            }
            if (bundle.containsKey(EXTRA_DEFAULT_USER_IDS)) {
                defaultUserIds = bundle.getString(EXTRA_DEFAULT_USER_IDS);
            }
            if (bundle.containsKey(EXTRA_DEFAULT_TASK_LV)) {
                defaultTaskLv = bundle.getInt(EXTRA_DEFAULT_TASK_LV, -1);
            }
            if (bundle.containsKey(EXTRA_DEFAULT_FINISH_TIME)) {
                defaultFinishTime = bundle.getString(EXTRA_DEFAULT_FINISH_TIME);
            }
        }
        setContentView(R.layout.activity_task_filter);
        manager = HttpManager.getInstance(this);
        initViews();
        initListView();
        loadData();
    }

    private void initViews() {
        setTitle("任务筛选");
        getTvArrowTitle().setVisibility(View.INVISIBLE);
        setTvRight("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveFilter();
            }
        });

        mRecyclerView = (XRecyclerView) this.findViewById(R.id.rv_task);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadingMoreEnabled(false);
    }

    private void initListView() {
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setLoadingMoreEnabled(false);
        mRecyclerView.setPullRefreshEnabled(false);
    }

    private void loadData() {
        DialogUtil.showLoadDiadlog(mContext);
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("listType", listType + "");
        manager.post(NetworkAPI.MISSION_ACTOR, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(getContext());
                if (code == 2) {
                    ToastOpt.createToast(getContext(), msg);
                    return;
                }
                List<UserBean> taskList = GsonUtil.paraeFromStringToList(value, UserBean.class);
                mAdapter = new FilterAdapter();
                mAdapter.setTaskType(defaultTaskType);
                mAdapter.setSelectedUserIds(defaultUserIds);
                mAdapter.setTaskLv(defaultTaskLv);
                mAdapter.setFinishTime(defaultFinishTime);
                mAdapter.setUserLists(taskList);
                mRecyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
            }

            @Override
            public void onFailure(String value) {
                ToastOpt.createToast(getContext(), "加载失败 " + value);
                DialogUtil.dismiss(getContext());
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
            }
        }, paramsDemo);
    }

    /**
     * 保存筛选条件
     */
    private void saveFilter() {
        DialogUtil.showLoadDiadlog(getContext());
        Intent intent = new Intent();
        int missionType = mAdapter.getSelectedTaskType();
        String userIds = mAdapter.getSelectedUserIds();
        int priority = mAdapter.getSelectedPriority();
        String finishTime = mAdapter.getSelectedDate();
        intent.putExtra(EXTRA_RESULT_TASK_TYPE, missionType);
        intent.putExtra(EXTRA_RESULT_USER_IDS, userIds);
        intent.putExtra(EXTRA_RESULT_PRIORITY, priority);
        intent.putExtra(EXTRA_RESULT_FINISH_TIME, finishTime);
        setResult(EXTRA_RESPONSE_CODE_SUCCESS, intent);
        DialogUtil.dismiss(getContext());
        finish();
    }
}
